package com.xiaoq.push.endpoint;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.push.entity.MsgCatalogueEntity;
import com.xiaoq.push.service.MsgCatalogueService;
import com.xiaoq.push.vo.MsgCatalogueVo;
import com.xiaoq.qiniu.QiNiuSpaceEnum;
import com.xiaoq.qiniu.UploadUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping(value = "/api/v1/user")
@Api(tags={"创建消息板块/获取用户消息板块下消息列表"})
public class MsgCatalogueEndpoint {
	
	private class MsgCatalogueVoListResponse extends GenericListResponse<MsgCatalogueVo>{}
	
	@Autowired
	private MsgCatalogueService msgCatalogueService;
	
	private static final Logger LOG = LoggerFactory.getLogger(MsgCatalogueEndpoint.class);
	
	@ApiOperation(value = "添加一个消息分类目录", notes = "添加一个消息分类目录")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "添加一个消息分类目录") })
	@PostMapping(value = "/msg_catalogues", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> createMsgCatalogue(
			@RequestParam(value = "msgCatalogueCode", required = true) String msgCatalogueCode,
			@RequestParam(value = "msgCatalogueName", required = true) String msgCatalogueName,
			@RequestParam(value = "file", required = true) MultipartFile file) {

		MsgCatalogueEntity msgCatalogueEntity = new MsgCatalogueEntity();
		String imgUrl = null;
		try {
			imgUrl = UploadUtil.easyUploadByte(file,
			        QiNiuSpaceEnum.XIAOQ_USER.getName().toLowerCase(), QiNiuSpaceEnum.XIAOQ_USER.getDomain());
			LOG.info("上传七牛云的图片地址："+imgUrl);
			msgCatalogueEntity.setMsgCatalogueCode(msgCatalogueCode);
			msgCatalogueEntity.setMsgCatalogueName(msgCatalogueName);
			msgCatalogueEntity.setThumbnailUrl(imgUrl);
			msgCatalogueService.createMsgCatalogue(msgCatalogueEntity);
			return GenericResponse.ok();
			
		} catch (IOException e) {
			LOG.error("七牛云上传的流出现问题！", e);
			return GenericResponse.ng("添加一个消息分类目录！");
		}

	}
	
	@ApiOperation(value = "获取消息目录列表", notes = "获取消息目录和每条目录最后一条消息")
	@ApiResponses({ @ApiResponse(code = 200, response = MsgCatalogueVoListResponse.class, message = "获取消息目录列表") })
	@GetMapping(value="/{userId}/msg_catalogues", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> findMsgCataloguesOfUser(@PathVariable(value = "userId", required = false)@ApiParam(value="userId",required=false)String userId){
		List<MsgCatalogueEntity> msgCatalogueEntityList;

        msgCatalogueEntityList = msgCatalogueService.getMsgCatalogues(userId);

        if (msgCatalogueEntityList == null){
        	msgCatalogueEntityList = new ArrayList<>();
        }
         return  MsgCatalogueVoListResponse.listNoCount(msgCatalogueEntityList);  
	}

}
